<template>
  <div>
    <el-row  type="flex" align="middle">
      <el-col :span="18">
        <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item v-for="(parent, index) of getParentList" :key="index" :to="{
        name: 'home', params: { simplePath: parent.simplePath }
      }">{{ parent.name }}</el-breadcrumb-item>
    </el-breadcrumb>
      </el-col>
      <el-col :span="6">
        <div class="grid-content bg-purple" v-show="parent!=''">
          <el-button type="primary" @click="uploadHandle(true)">上传文件</el-button>
          <el-button type="success" @click="uploadHandle(false)">上传文件夹</el-button>
        </div>
      </el-col>
    </el-row>
    <form hidden>
      <input ref="single" @change="upload" type="file" name="files">
      <input ref="dir" webkitdirectory @change="upload" type="file" name="files">
    </form>
    <el-table :data="dataList" border style="width: 100%;">
      <el-table-column prop="name" header-align="center" align="center" label="文件名">
        <template slot-scope="scope">
          <el-link v-if="isDir(scope.row)" @click="changeDir(scope.row)" type="success">{{ scope.row.name }}</el-link>
          <el-link v-else :href="scope.row.url" target="_blank">{{ scope.row.name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="250" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="updateHandle(scope.row)">修改</el-button>
          <el-button type="text" size="small" @click="remove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="重命名" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form :model="fileInfo">
        <el-form-item label="文件名称">
          <el-input v-model="fileInfo.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="rename">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import requests from '@/api/request';
export default {
  data() {
    return {
      dataList: [],
      fileInfo: {
        path: "",
        name: "",
        simplePath: "",
        type: "",
        url: "",
      },
      parent: "",
      dialogFormVisible: false,
    }
  },
  computed: {
    getParentList() {
      let home={ name: "home", simplePath: "" };
      if (!/^\/.+/.test(this.parent)) {
        return [home];
      }
      let list = this.parent.split("/");
      list.shift();
      let parentList = [];
      parentList.push(home)
      for (let i = 0; i < list.length; i++) {
        let name = list[i]
        let parent = parentList[i];
        let simplePath = parent.simplePath + "/" + name;
        parentList.push({ name, simplePath })
      }
      return parentList;
    }
  },
  methods: {
    upload($event){
      let files=$event.target.files;
      let form=new FormData()
      let config={headers: {'Content-Type':'multipart/form-data'}}
      form.append("parent",this.parent);
      console.log(files);
      for(let file of files){
        form.append("files",file);
      }
      requests.post("/view/upload",form,config).then(res=>{
        this.getList(this.parent);
          this.$message({
            message: '添加成功',
            type: 'success'
          });
      })
    },

    uploadHandle(isSingle){
      if(isSingle){
        this.$refs.single.click();
      }else{
        this.$refs.dir.click();
      }
      // this.$refs.uploadDir.$emit("click.native");
    },
    getList(simplePath = "") {
      requests({
        method: 'get',
        url: '/view/getList',
        params: {
          simplePath
        }
      }).then((data) => {
        this.dataList = data.list;
        this.parent = data.parent;
      });
    },
    isDir(res) {
      return res.type == "dir"
    },
    changeDir(res) {
      let route = Object.assign({}, this.$route);
      route.params.simplePath = res.simplePath;
      this.$router.push(route)
    },
    updateHandle(res) {
      this.dialogFormVisible = true
      this.fileInfo.path = res.path;
      this.fileInfo.name = res.name;
    },
    rename() {
      let { name, path } = this.fileInfo;
      requests({
        method: 'put',
        url: '/view/rename',
        params: {
          name, path
        }
      }).then(() => {
        this.getList(this.parent);
        this.dialogFormVisible = false;
        this.$message({
          message: '修改成功',
          type: 'success'
        });
      })
    },
    remove(res) {
      this.$confirm(`确定删除${res.name}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        requests({
          method: 'delete',
          url: '/view/delete',
          params: {
            path: res.path,
          }
        }).then(() => {
          this.getList(this.parent);
          this.$message({
            message: '删除成功',
            type: 'success'
          });
        })
      })
    }
  },
  watch: {
    $route: {
      immediate: true,
      handler(val) {
        this.getList(val.params.simplePath)
      }
    }
  },
}
</script>
<style scoped>

</style>